home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW re2c 1.1 / re.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-08  |  3.6 KB  |  184 lines  |  [TEXT/KAHL]

  1. #ifndef _re_h
  2. #define _re_h
  3.  
  4. // $Log: re.h,v $
  5. //Revision 1.1  1994/04/08  15:27:59  peter
  6. //Initial revision
  7. //
  8.  
  9. #include <iostream.h>
  10. #include "token.h"
  11. #include "ins.h"
  12.  
  13. struct CharPtn {
  14.     uint    card;
  15.     CharPtn    *fix;
  16.     CharPtn    *nxt;
  17. };
  18.  
  19. struct CharSet {
  20.     CharPtn    *fix;
  21.     CharPtn    *freeHead, **freeTail;
  22.     CharPtn    *rep[nChars];
  23.     CharPtn    ptn[nChars];
  24. };
  25.  
  26. class Range {
  27. public:
  28.     Range    *next;
  29.     uint    lb, ub;        // [lb,ub)
  30. public:
  31.     Range(uint l, uint u) : next(NULL), lb(l), ub(u)
  32.     { }
  33.     Range(Range &r) : next(NULL), lb(r.lb), ub(r.ub)
  34.     { }
  35.     friend ostream& operator<<(ostream&, const Range&);
  36.     friend inline ostream& operator<<(ostream&, const Range*);
  37. };
  38.  
  39. inline ostream& operator<<(ostream &o, const Range *r){
  40.     return r? o << *r : o;
  41. }
  42.  
  43. class RegExp {
  44. public:
  45.     uint    size;
  46. public:
  47.     virtual char *typeOf() = 0;
  48.     RegExp *isA(char *t)
  49.     { return typeOf() == t? this : NULL; }
  50.     virtual void split(CharSet&) = 0;
  51.     virtual void calcSize(Char*) = 0;
  52.     virtual uint fixedLength();
  53.     virtual void compile(Char*, Ins*) = 0;
  54.     virtual void display(ostream&) const = 0;
  55.     friend inline ostream& operator<<(ostream&, const RegExp&);
  56.     friend inline ostream& operator<<(ostream&, const RegExp*);
  57. };
  58.  
  59. inline ostream& operator<<(ostream &o, const RegExp &re){
  60.     re.display(o);
  61.     return o;
  62. }
  63.  
  64. inline ostream& operator<<(ostream &o, const RegExp *re){
  65.     return o << *re;
  66. }
  67.  
  68. class NullOp: public RegExp {
  69. public:
  70.     static char *type;
  71. public:
  72.     char *typeOf()
  73.     { return type; }
  74.     void split(CharSet&);
  75.     void calcSize(Char*);
  76.     uint fixedLength();
  77.     void compile(Char*, Ins*);
  78.     void display(ostream &o) const {
  79.     o << "_";
  80.     }
  81. };
  82.  
  83. class MatchOp: public RegExp {
  84. public:
  85.     static char *type;
  86.     Range    *match;
  87. public:
  88.     MatchOp(Range *m) : match(m)
  89.     { }
  90.     char *typeOf()
  91.     { return type; }
  92.     void split(CharSet&);
  93.     void calcSize(Char*);
  94.     uint fixedLength();
  95.     void compile(Char*, Ins*);
  96.     void display(ostream&) const;
  97. };
  98.  
  99. class RuleOp: public RegExp {
  100. private:
  101.     RegExp    *exp;
  102. public:
  103.     RegExp    *ctx;
  104.     static char *type;
  105.     Ins        *ins;
  106.     uint    accept;
  107.     Token    *code;
  108.     uint    line;
  109. public:
  110.     RuleOp(RegExp*, RegExp*, Token*, uint);
  111.     char *typeOf()
  112.     { return type; }
  113.     void split(CharSet&);
  114.     void calcSize(Char*);
  115.     void compile(Char*, Ins*);
  116.     void display(ostream &o) const {
  117.     o << exp << "/" << ctx << ";";
  118.     }
  119. };
  120.  
  121. class AltOp: public RegExp {
  122. private:
  123.     RegExp    *exp1, *exp2;
  124. public:
  125.     static char *type;
  126. public:
  127.     AltOp(RegExp *e1, RegExp *e2)
  128.     { exp1 = e1;  exp2 = e2; }
  129.     char *typeOf()
  130.     { return type; }
  131.     void split(CharSet&);
  132.     void calcSize(Char*);
  133.     uint fixedLength();
  134.     void compile(Char*, Ins*);
  135.     void display(ostream &o) const {
  136.     o << exp1 << "|" << exp2;
  137.     }
  138.     friend RegExp *mkAlt(RegExp*, RegExp*);
  139. };
  140.  
  141. class CatOp: public RegExp {
  142. private:
  143.     RegExp    *exp1, *exp2;
  144. public:
  145.     static char *type;
  146. public:
  147.     CatOp(RegExp *e1, RegExp *e2)
  148.     { exp1 = e1;  exp2 = e2; }
  149.     char *typeOf()
  150.     { return type; }
  151.     void split(CharSet&);
  152.     void calcSize(Char*);
  153.     uint fixedLength();
  154.     void compile(Char*, Ins*);
  155.     void display(ostream &o) const {
  156.     o << exp1 << exp2;
  157.     }
  158. };
  159.  
  160. class CloseOp: public RegExp {
  161. private:
  162.     RegExp    *exp;
  163. public:
  164.     static char *type;
  165. public:
  166.     CloseOp(RegExp *e)
  167.     { exp = e; }
  168.     char *typeOf()
  169.     { return type; }
  170.     void split(CharSet&);
  171.     void calcSize(Char*);
  172.     void compile(Char*, Ins*);
  173.     void display(ostream &o) const {
  174.     o << exp << "+";
  175.     }
  176. };
  177.  
  178. extern void genCode(ostream&, RegExp*);
  179. extern RegExp *mkDiff(RegExp*, RegExp*);
  180. extern RegExp *strToRE(SubStr);
  181. extern RegExp *ranToRE(SubStr);
  182.  
  183. #endif
  184.